import { ElLoading, ElNotification, ElMessageBox } from "element-plus";
import $store from "./$store";
import { $post } from "@/axios/request";

let loadCtx = null;
function openL(msg) {
  loadCtx = ElLoading.service({
    lock: true,
    text: msg || "加载中...",
    background: "rgba(0, 0, 0, 0.2)",
  });
}

function closeL() {
  if (loadCtx) {
    loadCtx.close();
  }
}

function openN(msg, type = "success") {
  //成功-success 警告-warning 失败-error
  ElNotification({
    title: "温馨提示",
    message: msg || "",
    type,
  });
}

function openC(msg, title, success, fail) {
  ElMessageBox.confirm(msg || "", title || "温馨提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
    autofocus: false,
    callback: () => {},
    beforeClose: (action, instance, done) => {
      if (action == "confirm") {
        success && success();
      } else {
        fail && fail();
      }
      done();
    },
  });
}

function loginSuccess() {
  // console.log(store);
}

function loginOut(router) {
  let primaryRouteList = $store.state.primaryRouteList || [];
  primaryRouteList.forEach((item) => {
    router.removeRoute(item.name);
  });

  $store.setStateData("menusData", null);
  $store.setStateData("firstUrl", "");
  $store.setStateData("primaryRouteList", []);
  $store.setStateData("scrollList", []);
  $store.setStateData("keepAliveList", []);
  $store.setStateData("childKeepAliveList", []);
  router.removeRoute("/layout");

  console.log(router.getRoutes());
}

export default {
  openL,
  closeL,
  openN,
  openC,
  loginSuccess,
  loginOut,
  $post,
};
